package Test03;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;
import java.util.Scanner;

public class Test03 {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("小明", "123"));
        users.add(new User("小红", "456"));
        users.add(new User("小勇", "789"));
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        boolean check = check(scanner.next(), users);
        if (check) {
            System.out.println("对不起,您注册的用户已经存在!请直接登录!");
        } else {
            System.out.println("请输入密码：");
            Scanner scanner1 = new Scanner(System.in);
            User u1 = new User(scanner.next(), scanner1.next());
            users.add(u1);
            System.out.println("注册成功");

            }


        Iterator<User> iter = users.iterator();
        while (iter.hasNext()){
            User u=iter.next();
            System.out.println(u.toString());}

    }

    public static boolean check(String name, ArrayList<User> users) {
        boolean flag = true;
        for (User u1 : users) {
            if (Objects.equals(u1.getName(), name)) {
                flag =true;
            }else {
                flag=false;
            }
        }
        if (flag) {
            return true;
        } else {
            return false;
        }
    }

}
